class BuyerController < ApplicationController
  #进行用户登录验证，通过后在session写入值
  def login
    is_seller = true
    is_login = false
    if session[:user_id]!=nil
      if params[:user_id]==session[:user_id]
        is_login = true
      end
    end
    if is_login==false
      user = WWW::Ali::Alisoft::User.new(params,session)
      if user.validate_user=='1'#登录成功
        session[:user_id]=user.user_id
        session[:app_instance_id]=user.app_instance_id
        session[:id]=WWW::Tkd::TbUser.save_from_alisoft(user,is_seller).id
      else
        render :text => "无效登录入口或请求过期，请重新检查并更新"
        return false
      end
    end
    return true
  end

  def data
    app_id='17887'
    cert_code='1b5d9fd0b09111ddbba8f4b86b8aa989'
    sip_url_pre='http://sip.alisoft.com/sip/rest'
    isv_url_pre='http://buyer.taokuaidi.cn/gwt/view'
    WWW::Ali.set_app_id app_id
    WWW::Ali.set_cert_code cert_code
    WWW::Ali.set_sip_url_pre sip_url_pre
    WWW::Ali.set_isv_url_pre isv_url_pre
    #首先进行用户验证
    if !login
      return
    end

    #获得Taobao的order数据
    params[:fields]='seller_nick,buyer_nick,iid,title,price,pic_path,num,created,type,tid,status,seller_rate,buyer_rate'
    trade = WWW::Ali::Taobao::Trade.new params,session
    items = trade.get_buy_items
    #如果Taobao没有登录成功，预先登录
    login_valid = WWW::Ali::Taobao::Tool.is_login(items)
    if login_valid.success==false
      redirect_to login_valid.url
      return
    end
    WWW::Tkd::TbBuyerOrder.save_from_tabao(items,session)#将Tabao订单数据导入到数据库中
    redirect_to(:controller=>'gwt',:action=>'view')
  end
end
